package net.lab1024.smartadmin.module.business.scenicSpot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.lab1024.smartadmin.common.anno.NoNeedLogin;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.common.heartbeat.StringUtil;
import net.lab1024.smartadmin.module.business.scenicSpot.domain.dto.ScenicSpotAddDTO;
import net.lab1024.smartadmin.module.business.scenicSpot.domain.dto.ScenicSpotUpdateDTO;
import net.lab1024.smartadmin.module.business.scenicSpot.domain.dto.ScenicSpotQueryDTO;
import net.lab1024.smartadmin.module.business.scenicSpot.domain.vo.ScenicSpotVO;
import net.lab1024.smartadmin.module.business.scenicSpot.domain.vo.ScenicSpotExcelVO;
import net.lab1024.smartadmin.module.business.scenicSpot.service.ScenicSpotService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import net.lab1024.smartadmin.util.SmartHttpUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * [  ]
 *
 * @author wldd
 * @version 1.0
 * @company 景区
 * @copyright (c)  景区Inc. All rights reserved.
 * @date 2020-12-26 14:25:23
 * @since JDK1.8
 */
@RestController
@Api(tags = {""})
public class ScenicSpotController extends BaseController {

    @Autowired
    private ScenicSpotService scenicSpotService;



    @GetMapping("testWx")
    @NoNeedLogin
    public ResponseDTO<String> testWx(@RequestParam(value = "code") String code) {

        System.out.println("code------------------------"+code);

        Map<String,String> param  = new HashMap<>();
        param.put("appid","wx1c4ff64d091fbaee");
        param.put("secret","76c4b024920d905ac292a413d8f56e6b");
        param.put("code",code);
        param.put("grant_type","authorization_code");

        String result = "";
        try {
            //通过code换取网页授权access_token
             result = SmartHttpUtil.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token", param, null);
            System.out.println("--------result" + result);

            JSONObject jsonObject = JSON.parseObject(result);
            String accessToken = (String) jsonObject.get("access_token");
            String openId =  (String) jsonObject.get("openid");
            String scope =  (String) jsonObject.get("scope");
//            拉取用户信息(需scope为 snsapi_userinfo)
            if(ObjectUtils.isNotEmpty(accessToken) && ObjectUtils.isNotEmpty(openId)) {
                Map<String,String> paramInfo  = new HashMap<>();
                paramInfo.put("access_token",accessToken);
                paramInfo.put("openid",openId);
                paramInfo.put("lang","zh_CN");
                result = SmartHttpUtil.sendGet("https://api.weixin.qq.com/sns/userinfo", paramInfo, null);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseDTO.succ().setData(result);
    }


    @ApiOperation(value = "分页查询",notes = "@author wldd")
    @PostMapping("/scenicSpot/page/query")
    public ResponseDTO<PageResultDTO<ScenicSpotVO>> queryByPage(@RequestBody ScenicSpotQueryDTO queryDTO) {
        return scenicSpotService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加",notes = "@author wldd")
    @PostMapping("/scenicSpot/add")
    public ResponseDTO<String> add(@RequestBody @Validated ScenicSpotAddDTO addTO){
        return scenicSpotService.add(addTO);
    }

    @ApiOperation(value="修改",notes = "@author wldd")
    @PostMapping("/scenicSpot/update")
    public ResponseDTO<String> update(@RequestBody @Validated ScenicSpotUpdateDTO updateDTO){
        return scenicSpotService.update(updateDTO);
    }

    @ApiOperation(value="批量删除",notes = "@author wldd")
    @PostMapping("/scenicSpot/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return scenicSpotService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wldd")
    @PostMapping("/scenicSpot/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<ScenicSpotExcelVO> scenicSpotList = scenicSpotService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, ScenicSpotExcelVO.class, scenicSpotList);
        downloadExcel("", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wldd")
    @PostMapping("/scenicSpot/export/all")
    public void exportAll(@RequestBody @Validated ScenicSpotQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<ScenicSpotExcelVO> scenicSpotList = scenicSpotService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, ScenicSpotExcelVO.class, scenicSpotList);
        downloadExcel("", workbook, response);
    }

}
